# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, İlker Kesen <ilker.kde@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.

import variables as scroppy
import piksemel
import urllib2

class Fans:
    def __init__(self, artist, track):
        (artist, track) = (artist.replace(" ", "%20"), track.replace(" ", "%20"))
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/track/%s/%s/fans.xml" % (scroppy.url, scroppy.scrobblerVersion, artist, track)).read())

    def artist(self):
        return self.data.getAttribute("artist")

    def track(self):
        return self.data.getAttribute("track")

    def fans(self):
        (fans, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                user   = variable.getAttribute("username")
                url    = variable.getTagData("url")
                image  = variable.getTagData("image")
                weight = variable.getTagData("weight")
                fans.append((user, url, image, weight))
        return fans

class Tags:
    def __init__(self, artist, track):
        (artist, track) = (artist.replace(" ", "%20"), track.replace(" ", "%20"))
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/track/%s/%s/toptags.xml" % (scroppy.url, scroppy.scrobblerVersion, artist, track)).read())

    def artist(self):
        return self.data.getAttribute("artist")

    def track(self):
        return self.data.getAttribute("track")

    def tags(self):
        (toptags, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                count  = variable.getTagData("count")
                url    = variable.getTagData("url")
                toptags.append((name, count, url))
        return toptags
